package com.complex.drools;

import java.io.FileReader;
import java.io.Reader;
import java.math.BigDecimal;

import org.drools.RuleBase;
import org.drools.RuleBaseFactory;
import org.drools.StatefulSession;
import org.drools.compiler.PackageBuilder;
import org.drools.event.DebugAgendaEventListener;
import org.drools.event.DebugWorkingMemoryEventListener;
import org.drools.rule.Package;

public class DroolsDemo {
	public static void main(String[] args) throws Exception {
		String filepath = "D:/work/project/own/complex/" +
				"complex.drools/src/main/resources/xinbang.drl";
		final Reader source = new FileReader(filepath);
		//规则包编译器
		final PackageBuilder builder = new PackageBuilder();
		builder.addPackageFromDrl(source);//增加规则

		//获得二进制规则包
		final Package pkg = builder.getPackage();
		//产生规则
		final RuleBase ruleBase = RuleBaseFactory.newRuleBase();
		ruleBase.addPackage(pkg);
		
		//规则调用器
		final StatefulSession session = ruleBase.newStatefulSession();
		session.addEventListener(new DebugAgendaEventListener());
		session.addEventListener(new DebugWorkingMemoryEventListener());
		
		//设置计算所需要的数据
		final Transport transport = new Transport();
		transport.setCharge(new BigDecimal(600));//设置保价
		transport.setRate(new BigDecimal(0.003));//设置费率
		transport.setMode("1");//设置运输方式
		
		//调用计算规则进行计算
		session.insert(transport);
		session.fireAllRules();
		System.out.println(transport.getFee());//取得计算结果
		
		session.dispose();
	}
}
